﻿#region Copyright(c) 1998-2012, Arnaud Colin Licence GNU GPL version 3
/* Copyright(c) 1998-2012, Arnaud Colin
 * All rights reserved.
 *
 * Licence GNU GPL version 3
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   -> Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   -> Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 */
#endregion

using System;
using System.Collections.Generic;
using System.IO;
#if NET35
using System.Linq;
#endif
using System.Text;
using System.Threading;
//using System.Threading.Tasks;
using System.Xml;
using System.Diagnostics;
using System.Reflection;
#if !WindowsCE
using System.Net.Mail;
#endif


namespace XoLib.CatchException
{
    /*
    /// <summary>
    /// simplifies the designation
    /// <example> CLogOnConsole.WriteMessage("KeyDown").Write(); </example>/// 
    /// </summary>
  //  public partial class CLogOnConsole : CLogger<CLogConsole> { }
    /// <summary>
    /// simplifies the designation
    /// <example> CLogOnMessage.WriteMessage("KeyDown").Write(); </example>/// 
    /// </summary>
  //  public partial class CLogOnMessage : CLogger<CLogMessage> { }
    /// <summary>
    /// simplifies the designation
    /// <example> CLogManager.WriteMessage("KeyDown").Write(); </example>/// 
    /// </summary>
  //  public partial class CLogOnFile : CLogger<CLogFile> {}
    */
  

    /// <summary>
    /// Write error and message in a MessageBox
    /// </summary>
    /// <example> CLogger CLogMessage .WriteMessage("Sample").Write(); </example>/// 
    public class CLogMessage : IActionLog
    {
        /// <summary>
        /// <para>
        /// <param name="Entry">LogEntry</param>
        /// </para>
        /// Call by the .Write(); 
        /// </summary>
        public void Write(LogEntry Entry)
        {
          // System.Windows.Forms.MessageBox(Entry.Event.OuterXml);
        }
    }

    /// <summary>
    /// Write error and message in the console window
    /// <example> CLogger CLogConsole .WriteMessage("Sample").Write(); </example>/// 
    /// </summary>
    public class CLogConsole : IActionLog
    {
        /// <summary>
        /// <para>
        /// <param name="Entry">LogEntry</param>
        /// </para>
        /// Call by the .Write(); 
        /// </summary>
        public void Write(LogEntry Entry)
        {
           Console.WriteLine(Entry.Doc.OuterXml);
        }
    }



    /// <summary>
    /// Write error and message in a file
    /// </summary>
    public class CLogFile : IActionLog
    {
        /// <summary>
        /// Urgence level of the message
        /// </summary>
        public static ushort Level = 0;

        /// <summary>
        /// Path and name of the file
        /// </summary>
#if !WindowsCE
        public static string Fichier = System.Diagnostics.Process.GetCurrentProcess().ProcessName.Trim(Path.GetInvalidFileNameChars()) + @".LOG";
#else
        public static string Fichier = @"CLogFile.LOG";
#endif

        private static Mutex mut = new Mutex();
        private static StreamWriter _fs = null;

        /// <summary>
        /// Constructor
        /// </summary>
        public CLogFile()
        {
        }

        /// <summary>
        /// <para>
        /// <param name="Entry">LogEntry</param>
        /// </para>
        /// Call by the .Write(); 
        /// </summary>
        public void Write(LogEntry Entry)
        {
            if(Entry.UrgenceLevel.HasValue && (Level != 0))
                if (Entry.UrgenceLevel.Value > Level)
                    return;
                
            WriteLine(Entry.Doc.OuterXml);
        }

        /// <summary>
        /// <para>
        /// <param name="s">string</param>
        /// </para>
        /// Write OuterXml in the file
        /// </summary>
        private static void WriteLine(string s)
        {
            mut.WaitOne();

            try
            {
                if (_fs == null)
                {
#if !WindowsCE
                    _fs = new StreamWriter(Directory.GetCurrentDirectory() + Fichier, true);
#else
                    _fs = new StreamWriter(Fichier, true);
#endif
                }

                _fs.WriteLine(s);
                _fs.Flush();
            }
            catch
            {
            }
            mut.ReleaseMutex();
        }
    }


#if !WindowsCE

    /// <summary>
    /// simplifies the designation
    /// <example> CLogOnEventLog.WriteMessage("KeyDown").Write(); </example>/// 
    /// </summary>
    [Serializable]
    public partial class CLogOnEventLog : CLogger<CLogEvent> { }


    /// <summary>
    /// Write error and message in the event log
    /// </summary>
    public class CLogEvent : IActionLog
    {

        static Process CurrentProcess = System.Diagnostics.Process.GetCurrentProcess();

        /// <summary>
        /// <para>
        /// <param name="Entry">LogEntry</param>
        /// </para>
        /// Call by the .Write();
        /// </summary>
        public void Write(LogEntry Entry)
        {

          int ec =  Convert.ToInt32(Entry.ReadXml("Code", "Entry"));
          int et = Convert.ToInt32(Entry.ReadXml("Type", "Entry"));

          if (!EventLog.SourceExists(CurrentProcess.ProcessName))
                EventLog.CreateEventSource(CurrentProcess.ProcessName, @"Application");

          EventLog.WriteEntry(CurrentProcess.ProcessName, Entry.Doc.OuterXml, (EventLogEntryType)(et == 0 ? 4 : et), ec);   
        }
    }
#endif

}
